{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 02, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> <font color=\"blue\"> Solutions for </font> Matrices: Two Dimensional Lists </h2>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task1\"></a>\n",
    "<h3> Task 1 </h3>\n",
    "\n",
    "Randomly create $ (3 \\times 4) $-dimensional matrices $ A $ and $ B $. \n",
    "\n",
    "The entries can be from the list $ \\{-5,\\ldots,5\\} $.\n",
    "\n",
    "Print the entries of both matrices.\n",
    "\n",
    "Find matrix $ C = 3A - 2B $, and print its entries.\n",
    "\n",
    "Verify the correctness your outcomes."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from random import randrange\n",
    "\n",
    "A = []\n",
    "B = []\n",
    "\n",
    "for i in range(3):\n",
    "    A.append([])\n",
    "    B.append([])\n",
    "    for j in range(4):\n",
    "        A[i].append(randrange(-5,6))\n",
    "        B[i].append(randrange(-5,6))\n",
    "\n",
    "print(\"A is\",A)\n",
    "print(\"B is\",B)\n",
    "\n",
    "C = []\n",
    "\n",
    "for i in range(3):\n",
    "    C.append([])\n",
    "    for j in range(4):\n",
    "        C[i].append( 3*A[i][j]-2*B[i][j])\n",
    "\n",
    "print(\"C is 3A - 2B\")\n",
    "print(\"C is\",C)        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task2\"></a>\n",
    "<h3> Task 2 </h3>\n",
    "\n",
    "Calculate $ M^T $ and $ N^T $ in python.\n",
    "\n",
    "Print all matrices and verify the correctness of your outcome."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "M = [\n",
    "    [-2,3,0,4],\n",
    "    [-1,1,5,9]      \n",
    "]\n",
    "N =[\n",
    "    [1,2,3],\n",
    "    [4,5,6],\n",
    "    [7,8,9]\n",
    "]\n",
    "\n",
    "# create the transpose of M as a zero matrix\n",
    "# its dimension is (4x2)\n",
    "MT = []\n",
    "for i in range(4):\n",
    "    MT.append([])\n",
    "    for j in range(2):\n",
    "        MT[i].append(0)\n",
    "\n",
    "# create the transpose of N as a zero matrix \n",
    "# its dimension is (3x3)\n",
    "NT = []\n",
    "for i in range(3):\n",
    "    NT.append([])\n",
    "    for j in range(3):\n",
    "        NT[i].append(0)\n",
    "\n",
    "# calculate the MT\n",
    "for i in range(2):\n",
    "    for j in range(4):\n",
    "        MT[j][i]=M[i][j] # check the indices\n",
    "\n",
    "print(\"M is\")\n",
    "for i in range(len(M)):\n",
    "    print(M[i])\n",
    "\n",
    "print()\n",
    "print(\"Transpose of M is\")\n",
    "for i in range(len(MT)):\n",
    "    print(MT[i])\n",
    "\n",
    "print()\n",
    "# calculate the NT\n",
    "for i in range(3):\n",
    "    for j in range(3):\n",
    "        NT[j][i]=N[i][j] # check the indices\n",
    "\n",
    "print(\"N is\")\n",
    "for i in range(len(N)):\n",
    "    print(N[i])\n",
    "\n",
    "print()\n",
    "print(\"Transpose of N is\")\n",
    "for i in range(len(NT)):\n",
    "    print(NT[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task3\"></a>\n",
    "<h3> Task 3 </h3>\n",
    "\n",
    "Find $ u' = N u $ in python for the following matrix $ N $ and column vector $ u $:\n",
    "\n",
    "$$\n",
    "    N = \\mymatrix{rrr}{-1 & 1 & 2 \\\\ 0 & -2 & -3 \\\\ 3 & 2 & 5 \\\\ 0 & 2 & -2} ~~~~~~\\mbox{and}~~~~~~ u = \\myrvector{2 \\\\ -1 \\\\ 3}.\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "N = [\n",
    "    [-1,1,2],\n",
    "    [0,-2,-3],\n",
    "    [3,2,5],\n",
    "    [0,2,-2]\n",
    "]\n",
    "\n",
    "u = [2,-1,3]\n",
    "\n",
    "uprime =[]\n",
    "\n",
    "print(\"N is\")\n",
    "for i in range(len(N)):\n",
    "    print(N[i])\n",
    "\n",
    "print()\n",
    "print(\"u is\",u)\n",
    "\n",
    "for i in range(len(N)): # the number of rows of N\n",
    "    S = 0 # summation of pairwise multiplications\n",
    "    for j in range(len(u)): # the dimension of u\n",
    "        S = S + N[i][j] * u[j]\n",
    "    uprime.append(S)\n",
    "\n",
    "print()\n",
    "print(\"u' is\",uprime)    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task4\"></a>\n",
    "<h3> Task 4 </h3>\n",
    "\n",
    "Find matrix $ K = M \\cdot N $ for given matrices\n",
    "$\n",
    "    M = \\mymatrix{rrr}{-1 & 0 & 1 \\\\ -2 & -1 & 2 \\\\ 1 & 2 & -2} ~~\\mbox{and}~~ \n",
    "    N = \\mymatrix{rrr}{0 & 2 & 1 \\\\ 3 & -1 & -2 \\\\ -1 & 1 & 0}.\n",
    "$\n",
    " \n",
    "This is a very challanging task. You may use triple nested for-loops. \n",
    "\n",
    "You may also consider to write a function taking two lists and returning their inner product."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# matrix M\n",
    "M = [\n",
    "    [-1,0,1],\n",
    "    [-2,-1,2],\n",
    "    [1,2,-2]\n",
    "]\n",
    "\n",
    "# matrix N\n",
    "N = [\n",
    "    [0,2,1],\n",
    "    [3,-1,-2],\n",
    "    [-1,1,0]\n",
    "]\n",
    "\n",
    "# matrix K\n",
    "K = []\n",
    "\n",
    "for i in range(3):\n",
    "    K.append([])\n",
    "    for j in range(3):\n",
    "        # here we calculate K[i][j]\n",
    "        # inner product of i-th row of M with j-th row of N\n",
    "        S = 0\n",
    "        for k in range(3):\n",
    "            S = S + M[i][k] * N[k][j]\n",
    "        K[i].append(S)\n",
    "        \n",
    "print(\"M is\")\n",
    "for i in range(len(M)):\n",
    "    print(M[i])\n",
    "    \n",
    "print()\n",
    "print(\"N is\")\n",
    "for i in range(len(N)):\n",
    "    print(N[i])\n",
    "\n",
    "print()\n",
    "print(\"K is\")\n",
    "for i in range(len(K)):\n",
    "    print(K[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task5\"></a>\n",
    "<h3> Task 5 </h3>\n",
    "\n",
    "Randomly define two $ (2 \\times 2) $-dimensional matrices $A$ and $ B $. \n",
    "\n",
    "Then, find $ C= AB-BA $. If $ C $ is not a zero matrix, then we are done.\n",
    "\n",
    "<i>Remark: With some probability, we may find a pair of $ (A,B) $ such that $ AB = BA $. \n",
    "    \n",
    "In this case, we should repeat our experiment. </i>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from random import randrange\n",
    "\n",
    "A = []\n",
    "B = []\n",
    "AB = []\n",
    "BA = []\n",
    "DIFF = []\n",
    "\n",
    "# create A, B, AB, BA, DIFF together\n",
    "for i in range(2):\n",
    "    A.append([])\n",
    "    B.append([])\n",
    "    AB.append([])\n",
    "    BA.append([])\n",
    "    DIFF.append([])\n",
    "    for j in range(2):\n",
    "        A[i].append(randrange(-10,10)) # the elements of A are random\n",
    "        B[i].append(randrange(-10,10)) # the elements of B are random\n",
    "        AB[i].append(0) # the elements of AB are initially set to zeros\n",
    "        BA[i].append(0) # the elements of BA are initially set to zeros\n",
    "        DIFF[i].append(0) # the elements of DIFF are initially set to zeros\n",
    "\n",
    "print(\"A =\",A)\n",
    "print(\"B =\",B)\n",
    "print() # print a line\n",
    "print(\"AB, BA, and DIFF are initially zero matrices\")\n",
    "print(\"AB =\",AB)\n",
    "print(\"BA =\",BA)\n",
    "print(\"DIFF =\",BA)\n",
    "\n",
    "# let's find AB\n",
    "for i in range(2):\n",
    "    for j in range(2):\n",
    "        # remark that AB[i][j] is already 0, and so we can directly add all pairwise multiplications\n",
    "        for k in range(2):\n",
    "            AB[i][j] = AB[i][j] + A[i][k] * B[k][j] # each multiplication is added\n",
    "\n",
    "print() # print a line\n",
    "print(\"AB =\",AB)\n",
    "\n",
    "# let's find BA\n",
    "for i in range(2):\n",
    "    for j in range(2):\n",
    "        # remark that BA[i][j] is already 0, and so we can directly add all pairwise multiplications\n",
    "        for k in range(2):\n",
    "            BA[i][j] = BA[i][j] + B[i][k] * A[k][j] # each multiplication is added\n",
    "\n",
    "print(\"BA =\",BA)\n",
    "\n",
    "# let's calculate DIFF = AB- BA\n",
    "for i in range(2):\n",
    "    for j in range(2):\n",
    "        DIFF[i][j] = AB[i][j] - BA[i][j]\n",
    "\n",
    "print() # print a line        \n",
    "print(\"DIFF = AB - BA =\",DIFF)        "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
